package com.lyf.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static List<String> readFile(String file) {
        List<String> list = new ArrayList<>();
        String str;
        try {
            LineNumberReader reader = new LineNumberReader(new FileReader(file));
            while ((str = reader.readLine()) != null) {
                if (!str.isEmpty()) {
                    list.add(str);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    public static void writeFile(String path, String data) throws IOException {
        File file = new File(path);
        // 创建文件
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        // 向文件写入内容
        writer.write(data);
        writer.flush();
        writer.close();
    }

    public static void writeFile(String file, List<String> list) throws IOException {
        File fout = new File(file);
        FileOutputStream fos = new FileOutputStream(fout, true);// 追加
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        for (String str : list) {
            bw.write(str);
            bw.newLine();
        }
        bw.close();
    }


    public static void main(String args[]) {

      List<String> list = FileUtil.readFile("E:\\work\\faxian\\data.txt");

      for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
} 

